package com.taobao.shopcart.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taobao.base.model.RestResponse;
import com.taobao.shopcart.mapper.CartMapper;
import com.taobao.shopcart.model.dto.CartDto;
import com.taobao.shopcart.model.po.Cart;
import com.taobao.shopcart.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.*;
import java.util.function.Function;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author erebus
 */
@Slf4j
@Service
public class CartServiceImpl implements CartService {


    @Autowired
    private CartMapper cartMapper;


    @Override
    public RestResponse<String> deleteCart(int cartId) {

        int i = cartMapper.deleteCart(cartId);
        if (i > 0) {
            return RestResponse.success("删除成功");
        }
        return RestResponse.validfail("删除失败");
    }

    @Override
    public RestResponse<List<Cart>> getCart(int userId) {

        List<Cart> list = cartMapper.getCart(userId);


        return RestResponse.success(list);
    }

    @Override
    @Transactional
    public RestResponse<String> addCart(CartDto cartDto, int userId) {

        this.cartMapper.deleteByGoodsId(userId, cartDto.getGoodsId());
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setImgUrl(cartDto.getImgUrl());
        cart.setGoodsId(cartDto.getGoodsId());
        cart.setQuantity(cartDto.getQuantity());
        cart.setUnitPrice(cartDto.getUnitPrice());
        cart.setTitle(cartDto.getTitle());
        cart.setStockStatus(1);

        int insert = this.cartMapper.insert(cart);
        if (insert > 0) {return RestResponse.success("添加成功");}
        return RestResponse.validfail("添加失败");
    }

    @Override
    public RestResponse<List<Integer>> cartToGoosd(List<Integer> selectedCartIds) {

        List<Integer> goodsIds = new ArrayList<>();

        for (Integer cartId : selectedCartIds) {
            int goodsId = cartMapper.getGoodsIdByCartId(cartId);
            goodsIds.add(goodsId);
        }

        return RestResponse.success(goodsIds);
    }

    @Override
    public RestResponse<List<Integer>> quantity(List<Integer> selectedCartIds) {
        List<Integer> quantitys = new ArrayList<>();

        for (Integer cartId : selectedCartIds) {
            int goodsId = cartMapper.getGoodsIdByCartId(cartId);
            quantitys.add(goodsId);
        }

        return RestResponse.success(quantitys);
    }

    @Override
    public boolean saveBatch(Collection<Cart> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Cart> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Cart> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Cart entity) {
        return false;
    }

    @Override
    public Cart getOne(Wrapper<Cart> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Optional<Cart> getOneOpt(Wrapper<Cart> queryWrapper, boolean throwEx) {
        return Optional.empty();
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Cart> queryWrapper) {
        return Collections.emptyMap();
    }

    @Override
    public <V> V getObj(Wrapper<Cart> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<Cart> getBaseMapper() {
        return null;
    }

    @Override
    public Class<Cart> getEntityClass() {
        return null;
    }


}
